#encoding=utf8
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC


def svm_classifier(train_data,train_label,test_data):
    '''
    input:train_data(ndarray):训练样本
          train_label(ndarray):训练标签
          test_data(ndarray):测试样本
    output:predict(ndarray):预测结果      
    '''
    #********* Begin *********#
    md = SVC()
    md.fit(train_data,train_label)

    #********* End *********#
    return md.predict(test_data)


#encoding=utf8
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import  train_test_split

#获取并处理鸢尾花数据
def create_data():
    iris = load_iris()
    df = pd.DataFrame(iris.data, columns=iris.feature_names)
    df['label'] = iris.target
    df.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'label']
    data = np.array(df.iloc[:100, [0, 1, -1]])
    #将标签为0的数据标签改为-1
    for i in range(len(data)):
        if data[i,-1] == 0:
            data[i,-1] = -1
    return data[:,:2], data[:,-1]

x,y = create_data()
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2,random_state=666)

predict = svm_classifier(x_train,y_train,x_test) 

acc = np.mean(y_test == predict)


if acc > 0.95 :
    print('正确率大于0.95')
else:
    print('正确率为:%.3f,请修改'%acc)
